package com.jushu.storbox;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jushu.storbox.manager.ActivityTaskManager;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.view.mdmview.IBaseDao;
import com.jushu.storbox.view.mdmview.IViewCallback;
import com.jushu.storbox.view.mdmview.ViewFactory;

public class ViewActivity extends Activity {

	public final static String TAG = "ViewActivity";
	
	private final static int ON_START = 0;
	private final static int ON_SUCCESS = 1;
	private final static int ON_PROGRESS = 2;
	private final static int ON_FAIL = 3;

	private String tmpPath = "";
	private String tmpFileName = "";

	private RelativeLayout mLayoutParent;
	private RelativeLayout mLayoutContent;
	private RelativeLayout mLayoutTitle;
	private RelativeLayout mViewBack;

//	private RelativeLayout mViewProgress;
	private static final int LAYOUT_TITLR_ID = 0xff;
//	private ProgressBar mProgressBar;
	private View view;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case ON_START:
				setContentView(mLayoutParent);
				LogUtil.i(TAG, "handler onStart...");
				break;
			case ON_SUCCESS:
				LogUtil.i(TAG, "handler onSuccess...");
				view = (View) msg.obj;
				// show
				if (null != view) {
					// mViewProgress.setAnimation(AnimationUtils.loadAnimation(ViewActivity.this,
					// android.R.anim.slide_out_right));
//					mViewProgress.setVisibility(View.GONE);
					RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
							LayoutParams.MATCH_PARENT,
							LayoutParams.MATCH_PARENT);
					// view.setAnimation(AnimationUtils.loadAnimation(ViewActivity.this,
					// android.R.anim.slide_in_left));
					params.addRule(RelativeLayout.CENTER_IN_PARENT,
							RelativeLayout.TRUE);
					mLayoutContent.addView(view, params);
					// setContentView(view);
				} else {
					ViewActivity.this.finish();
				}
				break;
			case ON_PROGRESS:
				int progress = (Integer) msg.obj;
				LogUtil.i(TAG, "handler onProgress:" + progress);
				break;
			case ON_FAIL:
				Exception e = (Exception) msg.obj;
				if (e.getMessage().equals("view not exist")) {
					Toast.makeText(getApplicationContext(),
							R.string.no_way_to_open_file, Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getApplicationContext(),
							R.string.file_open_fail, Toast.LENGTH_SHORT).show();
					LogUtil.e(TAG, "handler onFail...e:" + e.getMessage());
				}
				ViewActivity.this.finish();
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LogUtil.i(TAG, "onCreate()");
		ActivityTaskManager.getInstance().putActivity(TAG, ViewActivity.this);
		init();
		addListener();
		initView();
	}

	private void addListener() {
		mViewBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

	}

	private void init() {
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		mLayoutParent = new RelativeLayout(this);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		mLayoutParent.setLayoutParams(params);
		mLayoutTitle = new RelativeLayout(this);
		mLayoutTitle.setId(LAYOUT_TITLR_ID);
		mLayoutTitle.setBackgroundColor(ViewActivity.this.getResources()
				.getColor(R.color.black));
		mLayoutTitle.setVisibility(View.GONE);
		RelativeLayout.LayoutParams paramsTitle = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 50);
		paramsTitle.addRule(RelativeLayout.ALIGN_PARENT_TOP,
				RelativeLayout.TRUE);
		mLayoutParent.addView(mLayoutTitle, paramsTitle);
		mViewBack = new RelativeLayout(this);
		// mViewBack.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_button_bg));
		RelativeLayout.LayoutParams paramsBack = new RelativeLayout.LayoutParams(
				48, 50);
		paramsBack.addRule(RelativeLayout.ALIGN_PARENT_LEFT,
				RelativeLayout.TRUE);
		mLayoutTitle.addView(mViewBack, paramsBack);
		ImageView backImageView = new ImageView(this);
		backImageView.setImageResource(R.mipmap.back_arrowhead_im);
		RelativeLayout.LayoutParams paramsBackView = new RelativeLayout.LayoutParams(
				32, 32);
		paramsBackView.addRule(RelativeLayout.CENTER_IN_PARENT,
				RelativeLayout.TRUE);
		mViewBack.addView(backImageView, paramsBackView);
		mLayoutContent = new RelativeLayout(this);
//		mViewProgress = new RelativeLayout(this);
//		RelativeLayout.LayoutParams paramsProgressView = new RelativeLayout.LayoutParams(
//				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//		mLayoutContent.addView(mViewProgress, paramsProgressView);
		// mProgressBar = new ProgressBar(this, null,
		// android.R.attr.progressBarStyleHorizontal);
//		mProgressBar = new TextProgressBar(this);
//		RelativeLayout.LayoutParams paramsProgress = new RelativeLayout.LayoutParams(
//				LayoutParams.MATCH_PARENT, 120);
//		mProgressBar.setMax(100);
//		paramsProgress.addRule(RelativeLayout.CENTER_IN_PARENT,
//				RelativeLayout.TRUE);
//		mViewProgress.addView(mProgressBar, paramsProgress);
		mLayoutContent.setBackgroundColor(getResources()
				.getColor(R.color.black));
		RelativeLayout.LayoutParams paramsContent = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		paramsContent.addRule(RelativeLayout.BELOW, LAYOUT_TITLR_ID);
		mLayoutParent.addView(mLayoutContent, paramsContent);
	}

	private void initView() {
		// TODO Auto-generated method stub
		String mdmPath = getIntent().getStringExtra("view_mdm_path");
		IViewCallback callback = new IViewCallback() {

			@Override
			public void onSuccess(View view) {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = ON_SUCCESS;
				msg.obj = view;
				handler.sendMessage(msg);
			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = ON_START;
				handler.sendMessage(msg);
			}

			@Override
			public void onProgress(int progress) {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = ON_PROGRESS;
				msg.obj = progress;
				handler.sendMessage(msg);
			}

			@Override
			public void onFail(Exception e) {
				// TODO Auto-generated method stub
				Message msg = new Message();
				msg.what = ON_FAIL;
				msg.obj = e;
				handler.sendMessage(msg);
			}
		};
		ViewFactory viewFactory = new ViewFactory(ViewActivity.this, mdmPath,
				callback);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		LogUtil.i(TAG, "onDestroy()");
		super.onDestroy();
		//
		ActivityTaskManager.getInstance().removeActivity(TAG);
		//
		if (null != view) {
			LogUtil.i(TAG, "view has value that baseDao onDestory...");
			IBaseDao baseDao = (IBaseDao) view;
			baseDao.onDestory();
		} else {
			LogUtil.e(TAG, "view is null that baseDao can't onDestory...");
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		LogUtil.i(TAG, "onStop()");
		if (null != view) {
			LogUtil.i(TAG, "view has value that baseDao onStop...");
			IBaseDao baseDao = (IBaseDao) view;
			baseDao.onStop();
		} else {
			LogUtil.e(TAG, "view is null that baseDao can't onStop...");
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		LogUtil.i(TAG, "onResume()");
		if (null != view) {
			LogUtil.i(TAG, "view has value that baseDao onResume...");
			IBaseDao baseDao = (IBaseDao) view;
			baseDao.onResume();
		} else {
			LogUtil.e(TAG, "view is null that baseDao can't onResume...");
		}
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//			 Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

		} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//			 Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();

		}

	}

	//
	// @Override
	// public Object onRetainNonConfigurationInstance() {
	// // TODO Auto-generated method stub
	// return viewFactory;
	// }
	//
	// @Override
	// public Object getLastNonConfigurationInstance() {
	// return viewFactory;
	// }
	
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		overridePendingTransition(R.anim.left_in, R.anim.right_out);
//		overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);  
	}
}
